#pragma once
#include "BaseMat.h"
#define  SUBENTITYNUM 500
typedef struct  _FUNCSTR
{
	Ogre::SubEntity *  ent;
	Ogre::String	   val;
 	int			    textureNum;
	Ogre::Pass            *pas;
	Ogre::String					  constantName;
	Ogre::GpuConstantDefinition			* ConstantDefinition;
	int							      elementIndex;
	Ogre::GpuProgramParametersSharedPtr	  ProgramParameters;
	Ogre::String                      connectShaderName;
    //
	CBaseEditor *                     pentityEditor;
	int                               iGroupHead;
	Ogre::String                      PassColourName;

	bool (*HandleFunction)(int bledId,void * val);
}FUNCSTR;

typedef std::map<int,FUNCSTR> FUNCMAP;
typedef std::vector< Ogre::SubEntity *> SubEntityList;

class matUiEditor
{
public:
	matUiEditor(CBaseEditor * entityEditor,OgitorsSystem  *pSystem);
	~matUiEditor(void);

    void    init(const Ogre::String & matname);

	static bool      chanageColurValue(int bledId,void * val);
	static bool      chanageTexture(int bledId,void * val);
	static bool      chanageParameters(int bledId,void * val);
	bool             HandleCustomFunction(int BIND_ID,Ogre::String val);

	bool             connectShader(const Ogre::String &name,const Ogre::String & tempMat = "diffuse");
	void             createUI(int fData, int idata);
	void             deleteUI();

	void             addSubEntity(Ogre::SubEntity * ent);
	void             deleteSubEntity(Ogre::SubEntity * ent);

	void             loadMaxXml(Ogre::String &strloadMaxXml, Ogre::String &strMatBuffer);

	void             getMatBuffer(Ogre::String & maxSrc)
	{
		m_CurrentMat->getMatBuffer( maxSrc);
	}
    void             ExportMaxXml(Ogre::String &buffer);




	BaseMat            * m_CurrentMat;


protected:
	void     analyzeMat(const Ogre::MaterialPtr  mat,int &iData,int subEntityNum);
	void    SetPar(GpuProgramParametersSharedPtr params,int ifatherData,int & iData);
	void    creatColourValueUI(Ogre::Pass *pas,ColourValue val,void *caller,int idxparent, int& idxnew, Ogre::String namenew, bool valuelist = false);

    void    createTch(const Ogre::MaterialPtr  mat,int &iData,int subEntityNum);
    void    createPass(const Ogre::MaterialPtr  mat,int &iData,int subEntityNum);

private:


	MATLIST              m_ShaderMap;

	int                  m_iFData;
	int                  m_idata;
	int                  m_iDataHead;
	//int                  m_iDataLast;
	CBaseEditor *        m_entityEditor;
	FUNCMAP              m_FuncMap;
	SubEntityList           m_EntityList;
};

typedef std::vector<matUiEditor *> matEditorList;